home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / xhtmltools.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  5.9 KB  |  176 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import xml.sax.saxutils as xml
  5. import xml.dom as xml
  6. import re
  7. from urllib import quote, quote_plus, unquote
  8. from HTMLParser import HTMLParser
  9. import types
  10. import random
  11.  
  12. class XHTMLifier(HTMLParser):
  13.     
  14.     def convert(self, data, addTopTags = False, filterFontTags = False):
  15.         if addTopTags:
  16.             self.output = u'<html><head></head><body>'
  17.         else:
  18.             self.output = ''
  19.         self.stack = []
  20.         self.filterFontTags = filterFontTags
  21.         self.feed(data)
  22.         
  23.         try:
  24.             self.close()
  25.         except:
  26.             print 'DTV: unexpected error while parsing html data.'
  27.  
  28.         while len(self.stack) > 0:
  29.             temp = self.stack.pop()
  30.             self.output += u'</' + temp + '>'
  31.             continue
  32.             self
  33.         if addTopTags:
  34.             self.output += u'</body></html>'
  35.         
  36.         return self.output
  37.  
  38.     
  39.     def handle_starttag(self, tag, attrs):
  40.         if tag.lower() == 'br':
  41.             self.output += u'<br/>'
  42.         elif not tag.lower() == 'font' and self.filterFontTags:
  43.             self.output += u'<' + tag
  44.             for attr in attrs:
  45.                 if attr[1] == None:
  46.                     self.output += u' ' + attr[0] + u'=' + xml.sax.saxutils.quoteattr(attr[0])
  47.                     continue
  48.                 self
  49.                 self.output += u' ' + attr[0] + u'=' + xml.sax.saxutils.quoteattr(attr[1])
  50.             
  51.             self.output += u'>'
  52.         
  53.         self.stack.append(tag)
  54.  
  55.     
  56.     def handle_endtag(self, tag):
  57.         if tag.lower() != 'br' and len(self.stack) > 1:
  58.             temp = self.stack.pop()
  59.             if not tag.lower() == 'font' and self.filterFontTags:
  60.                 self.output += u'</' + temp + u'>'
  61.                 while temp != tag and len(self.stack) > 1:
  62.                     temp = self.stack.pop()
  63.                     self.output += u'</' + temp + u'>'
  64.                     continue
  65.                     self
  66.             
  67.         
  68.  
  69.     
  70.     def handle_startendtag(self, tag, attrs):
  71.         self.output += u'<' + tag + u'/>'
  72.  
  73.     
  74.     def handle_data(self, data):
  75.         data = data.replace(u'&', u'&')
  76.         data = data.replace(u'<', u'<')
  77.         self.output += data
  78.  
  79.     
  80.     def handle_charref(self, name):
  81.         self.output += u'&#' + name + ';'
  82.  
  83.     
  84.     def handle_entityref(self, name):
  85.         self.output += u'&' + name + ';'
  86.  
  87.  
  88.  
  89. def unescape(data):
  90.     return xml.sax.saxutils.unescape(data)
  91.  
  92.  
  93. def urlencode(data):
  94.     if type(data) == unicode:
  95.         data = data.encode('utf-8', 'replace')
  96.     else:
  97.         data = str(data)
  98.     return unicode(quote(data))
  99.  
  100.  
  101. def urldecode(data):
  102.     return unquote(data)
  103.  
  104.  
  105. def xhtmlify(data, addTopTags = False, filterFontTags = False):
  106.     x = XHTMLifier()
  107.     return x.convert(data, addTopTags, filterFontTags)
  108.  
  109. xmlheaderRE = re.compile('^\\<\\?xml\\s*(.*?)\\s*\\?\\>(.*)', re.S)
  110.  
  111. def fixXMLHeader(data, charset):
  112.     header = xmlheaderRE.match(data)
  113.     if header is None:
  114.         return '<?xml version="1.0" encoding="%s"?>%s' % (charset, data)
  115.     else:
  116.         xmlDecl = header.expand('\\1')
  117.         theRest = header.expand('\\2')
  118.         if xmlDecl.find('encoding'):
  119.             return data
  120.         else:
  121.             return '<?xml %s encoding="%s"?>%s' % (xmlDecl, charset, theRest)
  122.  
  123. HTMLHeaderRE = re.compile(u'^(.*)\\<\\s*head\\s*(.*?)\\s*\\>(.*?)\\</\\s*head\\s*\\>(.*)', re.I | re.S)
  124.  
  125. def fixHTMLHeader(data, charset):
  126.     header = HTMLHeaderRE.match(data)
  127.     if header is None:
  128.         return data
  129.     else:
  130.         headTags = header.expand('\\3')
  131.         if headTags.lower().find('content-type') != -1:
  132.             return data
  133.         else:
  134.             return header.expand('\\1<head \\2><meta http-equiv="Content-Type" content="text/html; charset=') + charset + header.expand('">\\3</head>\\4')
  135.  
  136.  
  137. def URLEncodeDict(orig):
  138.     output = []
  139.     for key in orig.keys():
  140.         if type(orig[key]) is types.ListType:
  141.             for value in orig[key]:
  142.                 output.append('%s=%s' % (quote_plus(key), quote_plus(value)))
  143.             
  144.         output.append('%s=%s' % (quote_plus(key), quote_plus(orig[key])))
  145.     
  146.     return '&'.join(output)
  147.  
  148.  
  149. def multipartEncode(postVars, files):
  150.     boundary = 'dp%s' % hex(random.getrandbits(64))[2:-1]
  151.     output = []
  152.     if postVars is not None:
  153.         for key, value in postVars.items():
  154.             output.append('--%s\r\n' % boundary)
  155.             output.append('Content-Disposition: form-data; name="%s"\r\n\r\n' % quote_plus(key))
  156.             if isinstance(value, unicode):
  157.                 value = value.encode('utf8', 'xmlcharrefreplace')
  158.             
  159.             output.append(value)
  160.             output.append('\r\n')
  161.         
  162.     
  163.     if files is not None:
  164.         for key in files.keys():
  165.             output.append('--%s\r\n' % boundary)
  166.             output.append('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (quote_plus(key), quote_plus(files[key]['filename'])))
  167.             output.append('Content-Type: %s\r\n\r\n' % files[key]['mimetype'])
  168.             output.append(files[key]['handle'].read())
  169.             output.append('\r\n')
  170.             files[key]['handle'].close()
  171.         
  172.     
  173.     output.append('--%s--' % boundary)
  174.     return (''.join(output), boundary)
  175.  
  176.